package im.eventservice;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import im.clusterhead.ClusterHeadInfo;
import im.general.Config;
import jade.core.behaviours.CyclicBehaviour;
import java.io.IOException;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.Vector;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Giovanni
 */
public class EventServiceLogBehaviour extends CyclicBehaviour {

    private Logger log = Logger.getLogger("Event Service");
    private FileHandler fh;
    private OurFormatter sf = new OurFormatter();
    private EventServiceAgent es;

    public EventServiceLogBehaviour(EventServiceAgent es) throws IOException {
        this.es = es;
        String slash = null;

        if(getOsName().equals("windows")){
            slash = "\\";    
        }
        else if(getOsName().equals("linux")){
            slash = "//";
        }

        fh = new FileHandler(Config.LOGGING_DIRECTORY + slash + Config.EVENT_SERVICE_LOG_FILE);
        fh.setFormatter(sf);
        log.addHandler(fh);
        log.setLevel(Level.ALL);
    }

    public int attachedNodeCounter() {
        int ret = 0;

        //es.heads tutti i cluster heads nella rete
        es.getCHList().size();


        return ret;

    }

    public String getTimeStampNow() {

        Calendar cal = new GregorianCalendar();
        int giorno = cal.get(Calendar.DAY_OF_MONTH);
        int mese = cal.get(Calendar.MONTH);
        int anno = cal.get(Calendar.YEAR);
        int ora = cal.get(Calendar.HOUR);
        int minuti = cal.get(Calendar.MINUTE);
        int secondi = cal.get(Calendar.SECOND);
        String orario;

        if (cal.get(Calendar.AM_PM) == 0) {
            orario = "A.M.";
        } else {
            orario = "P.M.";
        }

        return "" + giorno + "-" + (mese + 1) + "-" + anno + " " + ora + ":" + minuti + ":" + secondi + " " + orario + "";

    }

    public static String getOsName() {
        String os = "";
        if (System.getProperty("os.name").toLowerCase().indexOf("windows") > -1) {
            os = "windows";
        } else if (System.getProperty("os.name").toLowerCase().indexOf("linux") > -1) {
            os = "linux";
        } else if (System.getProperty("os.name").toLowerCase().indexOf("mac") > -1) {
            os = "mac";
        }

        return os;

    }

    void writeInFile() {

        es.updCHResourceStatus();
        Vector chNames = new Vector();
        Enumeration enumerationNames = es.heads.keys();

        while (enumerationNames.hasMoreElements()) {
            chNames.add(enumerationNames.nextElement());
        }

        int size = chNames.size();

        for (int i = 0; i < size; i++) {

            ClusterHeadInfo chi = (ClusterHeadInfo) es.heads.get(chNames.get(i));
            int attached = chi.getAttached_nodes();
            
            if (chi.getResourceStatus() != null) {
                log.log(Level.ALL, "" + getTimeStampNow() + ";" + chNames.get(i).toString() + ";"
                        + attached + ";" + chi.getResourceStatus().getStatusIndex());
            }

        }
    }

    @Override
    public void action() {
        //throw new UnsupportedOperationException("Not supported yet.");

        //mettiamo il log
        //stampiamo il numero dei cluster head etc etc...

        //log.log(Level.ALL,""+getTimeStampNow()+";"+es.heads.size()+";"+es.getResourceStatus().getStatusIndex()+";"+es.getResourceStatus().isPlugged()+";"+es.getResourceStatus().getChargeReminderMah()+";"+es.getResourceStatus().getFreeBWKbps()+";"+es.getResourceStatus().getFreeCPUMhz()+";"+es.getResourceStatus().getFreeDiskMB()+";"+es.getResourceStatus().getFreeRamMB()+";"+"status_idx/n_nodi_attaccati"+";"+"band/r_attached"+";");

        writeInFile();

        block(5000);

    }
}
